//
//  ImageStorgeUtil.m
//  NewsReader
//
//  Created by blacksun on 10-11-3.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "ImageStorgeUtil.h"


@implementation ImageStorgeUtil

+(BOOL)isImageExistInSystem:(NSString *)imagePath{
	NSFileManager *fileManager = [NSFileManager defaultManager];
	return [fileManager fileExistsAtPath:[ImageStorgeUtil getFullPathWithImagePath:imagePath]];
}
+(BOOL)saveImageData:(NSData *)aData toFilePath:(NSString *)imagePath{
	return [aData writeToFile:[ImageStorgeUtil getFullPathWithImagePath:imagePath] atomically:YES];
	
}
+(BOOL)deleteImageFromSystem:(NSString *)imagePath{
	NSFileManager *fileManager = [NSFileManager defaultManager];
	return [fileManager removeItemAtPath:[ImageStorgeUtil getFullPathWithImagePath:imagePath] error:nil];
}

//将http的图片url转换成可在文件系统中存储的图片名称
+(NSString *)getFilePathWithImagePath:(NSString *)imagePath{
	
	if ([imagePath hasPrefix:@"/cached/image/show"]) {
		NSArray *strs = [imagePath componentsSeparatedByString:@"?"];
		NSString *currentImageName = [[[NSString alloc] initWithFormat:@"%@.jpg",[strs objectAtIndex:1]] autorelease];
		return currentImageName;
	}
	return imagePath;
}

+(NSString *)getFullPathWithImagePath:(NSString *)imagePath{
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentPath = [paths objectAtIndex:0];
	NSString *imageFilePath = [documentPath stringByAppendingPathComponent:[ImageStorgeUtil getFilePathWithImagePath:imagePath]];
	return imageFilePath;
}

@end
